<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2016/10/30
 * Time: 下午8:33
 */
class WhiteController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('白名单管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('openid', 'input', '账号');
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = WhiteService::indexHeader();
		if(PubFunc::isAjax()){
			$result = WhiteService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=white/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'white/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	public function add(){
		$this->setTitle('添加白名单');
	
		$game = PubBase::getCurrentGame();
		$params = array();
		$params['g'] = $game;
		
		$modSpecialServer = new ModSpecialServer();
		$specialList_array = $modSpecialServer->select($params);
		$specialList = array();
		foreach ($specialList_array as $key=>$val){
			$specialList[$val['id']] = $val['name'];
		}
		
		$specialList = array('' => '请选择') + $specialList;
		
		$this->out['specialList'] = $specialList;
		
		$channelList = ChannelService::getChannel($params);
		$this->out['channelList'] = $channelList;
	}
	
	public function doAdd(){
		$count = WhiteService::repeat(0, 'openid', $this->R('openid'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该账号已经在白名单中', PubFunc::buildUrl("/oss/?r=white/index"));
		}else{
			$result = WhiteService::doAdd($_POST);
	
			if($result['state'] !== false){
				$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=white/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}	
	}
	

	public function delete(){
		$id = $this->R('id');
	    $result = WhiteService::delete($id);
	    if($result['state']  !== false){
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=white/index"));
	    }else{
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=white/index"));
	    }	 
	}


/**
    public function index(){
        $this->setTitle('白名单管理');

        $server = $this->R('server');
        $search = $this->R('search');
        $logic  = $this->R('logic');

        $platform = PubBase::getCurrentPlatform();
        $game = PubBase::getCurrentGame();
        $serverList = ServerService::getOptions($platform, $game);
        if(empty($server)){
            $server = key($serverList);
        }

        $logicList = WhiteService::logicServerList();

        $this->out['serverList'] = $serverList;
        $this->out['logicList']  = $logicList;

        $this->out['logic'] = $logic;
        $this->out['server'] = $server;
        $this->out['search'] = $search;

        if(!empty($search) && !empty($server) && !empty($logic)){
            $this->out['result'] = WhiteService::check($server, $search, $logic);
        }else{
            $this->out['result'] = '请将上述参数填写完整。';
        }
    }

    public function add(){
        $server = $this->R('server');
        $search = $this->R('search');
        $logic  = $this->R('logic');

        $this->outType = 'json';
        $this->out = WhiteService::add($server, $search, $logic);
    }

    public function del(){
        $server = $this->R('server');
        $search = $this->R('search');
        $logic  = $this->R('logic');

        $this->outType = 'json';
        $this->out = WhiteService::del($server, $search, $logic);
    }
**/


}